package org.opencms.free.balance;

import java.io.File;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.opencms.main.OpenCms;

/**
 * Clase que implementa la interfaz de persistencia de los objetos BalanceFreeNode.
 */
public class BalanceFreeNodeDaoImpl implements BalanceFreeNodeDao {

    /**
     * Atributo que almacena el objeto SessionFactory.
     */
    private static SessionFactory sessionFactory;

    /**
     * Directorio donde se encuentra al fichero de configuracion de Hibernate.
     */
    public static final String HIBERNATE_CONFIG = "classes/org/opencms/free/balance";

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            initSessionFactory();
        }
        return sessionFactory;
    }

    private static synchronized void initSessionFactory() {
        String rfsPath = OpenCms.getSystemInfo().getWebInfRfsPath();
        String rfsPathToFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(HIBERNATE_CONFIG);
        if (rfsPathToFile.startsWith(rfsPath)) {
            rfsPath = rfsPathToFile;
        } else {
            rfsPath = rfsPath + rfsPathToFile;
        }
        rfsPath = rfsPath + "/hibernate.cfg.xml";
        File file = new File(rfsPath);
        sessionFactory = new AnnotationConfiguration().configure(file).buildSessionFactory();
    }

    public static Session getSession() {
        return getSessionFactory().getCurrentSession();
    }

    /**
     * Constructor de la clase.
     */
    public BalanceFreeNodeDaoImpl() {
    }

    /**
     * Metodo que borra de la base de datos un objeto de tipo BalanceFreeNodeDao.
     */
    public void deleteNode(final BalanceFreeNode node) {
        Session session = getSession();
        Transaction transaction = session.beginTransaction();
        session.delete(node);
        transaction.commit();
    }

    /**
     * Metodo que busca un objeto de tipo BalanceFreeNode dado su nodeId.
     */
    public BalanceFreeNode findNode(final int id) {
        Session session = getSession();
        BalanceFreeNode node = (BalanceFreeNode) session.get(BalanceFreeNode.class, new Integer(id));
        return node;
    }

    /**
     * Metodo que obtiene todos los objetos de tipo BalanceFreeNode de la BD.
     */
    public List<BalanceFreeNode> findAllNodes() {
        Session session = getSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(BalanceFreeNode.class);
        List nodes = criteria.list();
        transaction.commit();
        return nodes;
    }

    /**
     * Metodo que guarda en base de datos un objeto de tipo BalanceFreeNodeDao.
     */
    public void saveNode(final BalanceFreeNode node) {
        Session session = getSession();
        Transaction transaction = session.beginTransaction();
        session.save(node);
        transaction.commit();
    }

    /**
     * Metodo que actualiza en base de datos un objeto de tipo BalanceFreeNodeDao.
     */
    public void updateNode(final BalanceFreeNode node) {
        Session session = getSession();
        Transaction transaction = session.beginTransaction();
        session.update(node);
        transaction.commit();
    }
}